var index = {
	campus:""
};

//课程列表
index.course = function(){	
	openLoad();
    var options = {
        courseStatus:"1",
        current:1,
        campus:index.campus,
        size:999
    };
    var ajax = new $ax(Feng.ctxPath+"/course/allParam", function(data){
    	Feng.log("课程信息查询成功");
        if (data.total > 0){           
            //图片过滤器    		
	        template.helper('pictureFormat', function (inp) {
	           if(inp){
	               return Feng.ctxPath+"/file/getImage/"+inp;
	           }
	           else{
	               return "themes/img/pd/course_defult.jpg";
	           }
	        });
            //价格
            template.helper('coursePriceFormat', function (inp) {
                if(inp!==""){
                    return numeral(inp).format('0,0.00');
                }
            });
            var outHtml = template("dataTmpl",data.records);
            $(".aui-list-product-box").html(outHtml);
        }
        closeLoad();
    },function(data){
    	closeLoad();
        Feng.log("加载信息失败!");
    });
    ajax.setData(options);
    ajax.start();
};

//banner
index.banner = function(){
    var ajax = new $ax(Feng.ctxPath+"/banner/selectParam", function(data){
    	Feng.log("banner加载成功");
        if (data.success > 0){           
            //图片过滤器
            template.helper('imgFormat', function (inp) {
               if(inp){
                   return Feng.ctxPath+"/file/getImage/"+inp;
               }
               else{//默认图片
                   return "themes/img/banner/adv1.png"
               }
            });
            var outHtml = template("bannerTmpl",data.obj);
            $(".aui-banner-wrapper").html(outHtml);
        }
    },function(data){
        Feng.log("banner加载失败");
    });
    ajax.start();
};

//获取用户ID
index.getUserId = function(){
	var args = getQueryStringArgs(),
		userId = args["userId"];
		
	if(userId)	
	{
		localStorage.setItem("userId",userId);
		//修改首页url清空userId
		var href = window.location.origin + window.location.pathname;
		history.replaceState({}, "", href);	
	}
		
}

//校区树形
index.tree = function(){
	var ajax = new $ax(Feng.ctxPath+"/school/getTree", function(data){
		Feng.log("校区加载成功");
		if (data.obj != null && data.obj != ""){
			var cityPicker3 = new mui.PopPicker({
				layer: 3
			});
			cityPicker3.setData(data.obj);
			var showCityPickerButton = document.getElementById('J_Address');
			var cityResult3 = document.getElementById('campus');
			showCityPickerButton.addEventListener('tap', function(event) {
				cityPicker3.show(function(items) {
					var text = (items[2] || {}).text;
					if(text!== undefined && text!== null)
					{
						if(text.length>6){
							cityResult3.innerText = text.substring(0,5)+"...";
						}
						else{
							cityResult3.innerText = text;
						}
						
						if(index.campus !== items[2].value){
							index.campus =items[2].value;
							index.course();							
						}
						
						
					}

					//返回 false 可以阻止选择框的关闭
					//return false;
				});
			}, false);
		}
	},function(data){
		Feng.log("校区加载失败");
	});
	ajax.start();
};

	
//搜索课程
//$("#form-search").on('submit',function () {
$("#aui-header-search").keypress(function () {
    if (event.keyCode == 13){
    	var coursename = $(this).val();
		window.location.href = "course_list.html?campus={0}&courseName={1}".format(index.campus,coursename);	
    }
});

//$("#aui-header-search").on("click",function(){
//	window.location.href = "course_list.html?campus={0}".format(index.campus);	
//  var coursename = $(this).val();
//});


$(function(){
	index.getUserId();

	index.course();
	index.banner();
	index.tree();

	$("a[name='classSearch']").click(function (event) {
		var courseClass =$(this.children[1]).html();
		if(courseClass=="课外辅导"){
			window.location.href = "course_list.html?courseClass=1";
		}else if(courseClass=="研学旅行"){
			window.location.href = "course_list.html?courseClass=2";
		}else if(courseClass=="冬夏令营"){
			window.location.href = "course_list.html?courseClass=3";
		}else if(courseClass=="线上辅导"){
			window.location.href = "course_list.html?courseClass=4";
		}
	})
	
	lazyloadImg();

console.log(YDUI.device)
});
